package com.wch.SpringBootTestDemo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;


@Controller
@RequestMapping("/springboot")
@SuppressWarnings(value = { "all" })
public class ReadListController {

	private ReadingListRepository readingListRepository;
	@Autowired
	public ReadListController(ReadingListRepository readingListRepository) {
		this.readingListRepository = readingListRepository;
	}

	@RequestMapping(value = "/{reader}", method = RequestMethod.GET)
	public String readersBooks(@PathVariable("reader") String reader, Model model) {
		System.out.println("GET:"+reader.toString());
	/*	List<Book> readingList = readingListRepository.findByReader(reader);*/
		List<Book> readingList =  new ArrayList<Book>();
		Book book = new Book();
		book.setAuthor("wch");
		book.setDescription("123");
		book.setId((long) 12);
		book.setIsbn("gs");
		book.setReader("sew");
		book.setTitle("yrr");
		readingList.add(book);
		if (readingList != null) {
			model.addAttribute("books", readingList);
		}
		System.out.println("readingList="+readingList);
		System.out.println("model="+model);
		return "readingList";
	}
	
	@RequestMapping(value = "/testVue", method = RequestMethod.GET)
	public String testVue() {
		return "testVue";
	}
	
	@RequestMapping(value = "/testVue2", method = RequestMethod.GET)
	public String testVue2() {
		return "/vuePage/testVue2";
	}


	@RequestMapping(value = "/{reader}", method = RequestMethod.POST)
	public String addToReadingList(@PathVariable("reader") String reader, Book book) throws URIException, NullPointerException {
		//接口模块测试路径
		//String urlPath = "http://192.168.137.1:8081/interface/restful64/restPonNet/autoSendData";
		//String urlPath = "http://134.224.171.47:8085/interface-jx/restful64/restPonNet/autoSendData";
		String urlPath = "http://134.252.3.246:8081/interface/restful64/restPonNet/autoSendData";
		
		//String urlPath = "http://134.224.164.13:8801/interface-jx/restful64/restPonNet/autoSendData";

		//String urlPath = "http://192.168.0.106:8081/interface/restful64/restPonNet/autoSendData";
		//String urlPath = "http://134.224.118.83/device/rep_info";
		//String urlPath = "http://192.168.0.106:8081/interface/restful64/restPonNet/ponSyncAttr";
		/*String urlPath = "http://134.224.118.83/device/rep_info";*/

		URI url = new URI(urlPath, false, "UTF-8");
		HttpClient httpClient = new HttpClient();
		httpClient.getParams().setContentCharset("UTF-8");
		PostMethod postMethod = new PostMethod(url.toString());
	//	postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		Map<String, Object> param = new HashMap<String, Object>();

		//OLT 新增回调
/*		param.put("BRANCH", "城东分局");
		param.put("DEVICE_ID", "331000000000100282212170");
		param.put("EMS_ID", "9656");
		param.put("FACILITY_NAME", "测试机房");
		param.put("FACILITY_SPEC_ID", "机房");
		param.put("FLAG", "成功");
		param.put("REGION", "南昌市");
		param.put("SOURCE", "PON");
		param.put("TABLE_NAME", "X_EMS_DEVICE_OLT");
		param.put("NOTES", "");
		param.put("ORGANIZATION_1_RELATE_DEVICE", "");*/
		
		//OLT 修改回调
		/*		param.put("BRANCH", "城东分局");
				param.put("DEVICE_ID", "331000000000100282212170");
				param.put("EMS_ID", "9656");
				param.put("FACILITY_NAME", "测试机房");
				param.put("FACILITY_SPEC_ID", "机房");
				param.put("FLAG", "成功");
				param.put("REGION", "南昌市");
				param.put("SOURCE", "PON");
				param.put("TABLE_NAME", "X_EMS_DEVICE_OLT");
				param.put("NOTES", "");
				param.put("ORGANIZATION_1_RELATE_DEVICE", "");*/
				
				//OLT 删除回调
	/*			param.put("DEVICE_ID", "331000000000100282212170");
				param.put("EMS_ID", "9656");
				param.put("FLAG", "成功");
				param.put("SOURCE", "PON");
				param.put("TABLE_NAME", "X_EMS_DEVICE_OLT");
				param.put("NOTES", "");
				param.put("ORGANIZATION_1_RELATE_DEVICE", "");*/

/*		//板卡 新增回调
		param.put("BOARD_ID", "NA-0-9");
		param.put("BRANCH", "");
		param.put("DEVICE_ID", "331000000000100282212170");
		param.put("EMS_NE_ID", "9656");
		param.put("FLAG", "成功");
		param.put("REGION", "南昌市");
		param.put("SLOT_CODE", "9");
		param.put("SLOT_ID", "123");
		param.put("SOURCE", "PON");
		param.put("TABLE_NAME", "X_EMS_SLOT_CARD");
		param.put("NOTES", "");*/
		

/*		param.put("SHARDING_ID", "797");
		param.put("NAME", "测试--龙勾Z01/华为技术有限公司MA5683T/GZ-华为技术有限公司MA5683T/GZ-华为技术有限公司MA5683T/GZ-OLT0001");
		param.put("MANUFACTOR_ID", "华为");
		param.put("TELNET_IP", "172.21.70.41");
		param.put("SOURCE", "PON");
		param.put("TABLE_NAME", "X_EMS_DEVICE_OLT");
	//	param.put("CODE", "龙勾Z01/华为技术有限公司MA5683T/GZ-华为技术有限公司MA5683T/GZ-华为技术有限公司MA5683T/GZ-OLT0001");
		param.put("ACTIONTYPE", "删除");
		param.put("MODEL_EMS", "MA5683T");
		param.put("MODIFYTIME", "2019-01-08 00:56:09");
		param.put("EMS_ID", "9662");
		param.put("NETWORK_LAYER_ID", "接入层");
		param.put("CREATETIME", "2014-12-06 21:06:41");
		param.put("CODEINEMS", "LongGou-5680T-GPON");
		param.put("DEVICE_ID", "361100000000000025092261");
		param.put("ORGANIZATION_EMS","");
		param.put("HARDWAREVERSION", "");
		param.put("INTO_NETWORK_DATE", "2014-12-06 21:06:41");
		param.put("IS_DOUBLE_COUPLET", "否");
		param.put("SOFTWAREVERSION", "11");*/
	/*	
 
		param.put("BOARD_ID", "");
		param.put("DEVICE_CODE", "资源同步测试OLT");
		param.put("SHARDING_ID", "793");
		param.put("EMS_NE_ID", "9662");
		param.put("SOURCE", "PON");
		param.put("SLOT_TYPE", "母槽");
		param.put("CARD_MODEL_ID", "H808EPSD");
		param.put("SUB_SLOT_ID", "");
		param.put("DEVICE_NAME", "资源同步测试OLT");
		param.put("SLOT_ID", "361100000000000025092956");
		param.put("ACTIONTYPE", "修改");
		param.put("TABLE_NAME", "X_EMS_SLOT_CARD");
		param.put("MODIFYTIME", "2016-07-18 16:36:35");
		param.put("SLOT_CODE", "1");
		param.put("DEVICE_TELNET_IP", "10.23.50.152");
		param.put("CREATETIME","2016-07-19 17:40:48");
		param.put("CODEINEMS", "HZ-TL-DongH-OLT-1.MAN.C300");
		param.put("DEVICE_ID", "361100000000000025092261");
		param.put("MODEL_EMS", "MA5680T");
		param.put("MANUFACTOR_ID", "华为");*/
		

		param.put("ORGANIZATION_EMS", "");
		param.put("NAME_CODE", "");
		param.put("SHARDING_ID", "794");
		param.put("NAME", "");
		param.put("HARDWAREVERSION", "");
		param.put("MANUFACTOR_ID", "");
		param.put("TELNET_IP", "");
		param.put("INTO_NETWORK_DATE", "");
		param.put("SOURCE", "PON");
		param.put("TABLE_NAME", "X_EMS_DEVICE_OLT");
		param.put("CODE", "");
		param.put("ACTIONTYPE", "删除");
		param.put("MODEL_EMS", "");
		param.put("MODIFYTIME","2019-09-03 11:43:10");
		param.put("IS_DOUBLE_COUPLET", "");
		param.put("EMS_ID", "2802");
		param.put("SOFTWAREVERSION", "");
		param.put("NETWORK_LAYER_ID", "接入层");
		param.put("CREATETIME", "");
		param.put("CODEINEMS", "");
		param.put("DEVICE_ID", "361100000000000001752882");

		
		
	/*	param.put("MODEL_EMS", "比尔");
		param.put("ORGANIZATION_EMS", "组织机构");
		param.put("MANUFACTOR_ID", "厂商");*/
	/*	param.put("NM_CODE", "4567");
		param.put("PHYSICAL_STATE", "4567");
		param.put("SOFTWARE_VERSION", "4567");
		param.put("HARDWARE_VERSION", "4567");*/
		Object json = JSONObject.toJSON(param);

		postMethod.setRequestBody(json.toString());
		String result = "";
		/*System.out.println("POST:"+reader.toString());
		book.setReader(reader);
		System.out.println("reader="+reader.toString());
		System.out.println("book="+book.toString());
		readingListRepository.save(book);*/
		try {
			System.out.println("开始调用接口uri" + url);
			System.out.println("开始调用接口时间" + System.currentTimeMillis());
			httpClient.executeMethod(postMethod);
			System.out.println("接口结束接口时间" + System.currentTimeMillis());
			result = "[状态码]:" + postMethod.getStatusCode();
			System.out.println(result);
			if (200 == postMethod.getStatusCode()) {
				System.out.println("接口服务调用成功！");
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				InputStream in = postMethod.getResponseBodyAsStream();
				int len = 0;
				byte[] buf = new byte[1024];
				while ((len = in.read(buf)) != -1) {
					out.write(buf, 0, len);
				}
				String responseMsg = out.toString("UTF-8");
				System.out.println("接口调用成功返回信息:" + responseMsg);
			} else {
				System.out.println("接口调用服务失败！");
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			postMethod.releaseConnection();
		}
		
		return "成功";
	}

}
